package hk;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;

public class MousListener extends MouseAdapter implements CS, ActionListener {

    private int x,y;//棋子像素坐标
    private int ix,iy;//用户棋子坐标
    private int cx,cy;//电脑棋子坐标
    private int n=1;//下黑棋或白棋
    public int inte=0;//棋子数目
    private int prex,prey;//上一个棋子的坐标
    private int count1,count2,countS,countX,countZS,countYX,countZX,countYS=0;
    private int modeValue;
    private int colorValue;
    String zuo="";
    String you="";
    String shang="";
    String xia="";
    String zuoshang="";
    String zuoxia="";
    String youshang="";
    String youxia="";
    private boolean a=true;//玩家对战下棋顺序
    private boolean b=true;//是否在同一个位置
    private Color userColor;//用户棋子颜色
    private Color computerColor;//电脑棋子颜色
    private Color color;//玩家对战颜色交替下棋
    public Qizi qizis[][]=new Qizi[line][line];
    private int chessValue[][]=new int[line][line];//权值表
    private JLabel j=new JLabel("请选择对战模式");
    private JLabel j2=new JLabel("请选择棋子颜色");

    JFrame jFrame;//悔棋重绘传参
    Graphics graphics;
    HashMap<String, Integer> hashMap=new HashMap<String, Integer>();


    public MousListener(Graphics graphics, Qizi qizi[][], JFrame jFrame, int modeValue, int colorValue) {
        this.graphics = graphics;
        qizis =qizi;
        this.jFrame=jFrame;
        this.modeValue = modeValue;
        this.colorValue = colorValue;
        if(colorValue ==1) {
            userColor = Color.black;
            computerColor =Color.white;
        }else {
            userColor =Color.white;
            computerColor =Color.black;
        }
    }



    public void mouseClicked(MouseEvent e) {
        x=e.getX();
        y=e.getY();
        int x1=x-x0;
        int y1=y-y0;

        //x轴坐标
        for(ix=0;x1>0;ix++) {
            x1-=size;
        }
        x1+=size;
        x1-=size/2;
        ix--;
        if(x1<=0) {
            x=x0+ix*size;
        }else
            x=x0+(++ix)*size;
        //y轴坐标
        for(iy=0;y1>0;iy++) {
            y1-=size;
        }
        y1+=size;
        y1-=size/2;
        iy--;
        if(y1<=0) {
            y=y0+iy*size;
        }else
            y=y0+(++iy)*size;
        //判断是否在同一个地方
        b=true;
        if(qizis[ix][iy]!=null) {
            JLabel jLabel =new JLabel("不能下在同一个地方！");
            JOptionPane.showMessageDialog(jFrame, jLabel, "警告", JOptionPane.WARNING_MESSAGE);
            b=false;
        }
        //--------------------
        //画圆及重构，一个黑棋一个白棋
        //--------------------
        if(b) {
            if(modeValue ==1) {
                graphics.setColor(userColor);
                //绘制用户的棋子
                graphics.fillOval(x-size/2, y-size/2, size, size);
                prex=ix;
                prey=iy;
                Qizi qizi=new Qizi(graphics, userColor,ix,iy);
                qizis[ix][iy]=qizi;
                inte++;
                System.out.println("用户下棋");

                computerChess();
                int qzmax=0;
                for(int b=0;b<line;b++) {
                    for(int a =0;a<line;a++) {
                        if(chessValue[a][b]>qzmax) {
                            qzmax=chessValue[a][b];
                            cx=a;
                            cy=b;
                        }
                    }
                }

                graphics.setColor(computerColor);
                //电脑下棋
                graphics.fillOval(x0+cx*size-size/2, y0+cy*size-size/2, size, size);
                Qizi qizi2=new Qizi(graphics, computerColor,cx,cy);
                qizis[cx][cy]=qizi2;
                inte++;
                System.out.println("电脑下棋");

                for(int b=0;b<line;b++) {
                    for(int a =0;a<line;a++) {
                        chessValue[a][b]=0;

                    }
                }

                //判断输赢
                if(zuo(ix,iy, userColor)+you(ix,iy, userColor)>=4||shang(ix,iy, userColor)+xia(ix,iy, userColor)>=4
                        ||zuoshang(ix, iy, userColor)+youxia(ix, iy, userColor)>=4||zuoxia(ix, iy, userColor)+youshang(ix, iy, userColor)>=4) {
                    JLabel jLabel =new JLabel("玩家获胜！");
                    JOptionPane.showMessageDialog(jFrame, jLabel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
                }else {
                    init();
                    if((zuo(cx,cy, computerColor)+you(cx,cy, computerColor)>=4||shang(cx,cy, computerColor)+xia(cx,cy, computerColor)>=4
                            ||zuoshang(cx, cy, computerColor)+youxia(cx, cy, computerColor)>=4||zuoxia(cx, cy, computerColor)+youshang(cx, cy, computerColor)>=4) ){
                        JLabel jLabel =new JLabel("电脑获胜！");
                        JOptionPane.showMessageDialog(jFrame, jLabel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
                    }else {
                        init();
                    }
                }
            }else {
                if(a) {
                    color = userColor;
                    a=false;
                }else {
                    color= computerColor;
                    a=true;
                }
                graphics.setColor(color);
                graphics.fillOval(x-size/2, y-size/2, size, size);
                prex=ix;
                prey=iy;
                Qizi qizi=new Qizi(graphics, color,ix,iy);
                qizis[ix][iy]=qizi;
                inte++;


                //判断输赢
                if(zuo(ix,iy,color)+you(ix,iy,color)>=4||shang(ix,iy,color)+xia(ix,iy,color)>=4
                        ||zuoshang(ix, iy,color)+youxia(ix, iy,color)>=4||zuoxia(ix, iy,color)+youshang(ix, iy,color)>=4) {
                    JLabel jLabel =new JLabel("白棋获胜！");
                    JLabel jlabel2 =new JLabel("黑棋获胜！");
                    if(color==Color.white)
                        JOptionPane.showMessageDialog(jFrame, jLabel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
                    else
                        JOptionPane.showMessageDialog(jFrame, jlabel2, "游戏结束", JOptionPane.PLAIN_MESSAGE);
                }else {
                    //如果没有赢重新置0重新计算
                    init();
                }
            }


        }





    }

    public int zuo(int x,int y,Color c) {//向左找
        int a=x;
        for(int i=1;i<5;i++) {
            a--;
            if(a<0|| qizis[a][y]==null) {
                break;
            }else if(qizis[a][y].getColor()==c)
                count1++;
            else
                break;
        }
        return count1;
    }

    public int you(int x,int y,Color c){//向右找
        int a =x;
        for(int i=1;i<5;i++) {
            a++;
            if(a==15|| qizis[a][y]==null) {
                break;
            }else if(qizis[a][y].getColor()==c)
                count2++;
            else
                break;
        }
        return count2;
    }

    public int xia(int x,int y,Color c) {//向下找
        int a=y;
        for(int i=1;i<5;i++) {
            a++;
            if(a==15|| qizis[x][a]==null) {
                break;
            }else if(qizis[x][a].getColor()==c)
                countX++;
            else
                break;
        }
        return countX;
    }

    public int shang(int x,int y,Color c){//向上找
        int a =y;
        for(int i=1;i<5;i++) {
            a--;
            if(a<0|| qizis[x][a]==null) {
                break;
            }else if(qizis[x][a].getColor()==c)
                countS++;
            else
                break;
        }
        return countS;
    }

    public int zuoshang(int x,int y,Color c) {//向左上找
        int a=x;
        int b=y;
        for(int i=1;i<5;i++) {
            a--;
            b++;
            if(a<0||b==15|| qizis[a][b]==null) {
                break;
            }else if(qizis[a][b].getColor()==c)
                countZS++;
            else
                break;
        }
        return countZS;
    }

    public int youxia(int x,int y,Color c) {//向右下找
        int a=x;
        int b=y;
        for(int i=1;i<5;i++) {
            a++;
            b--;
            if(b<0||a==15|| qizis[a][b]==null) {
                break;
            }else if(qizis[a][b].getColor()==c)
                countYX++;
            else
                break;
        }
        return countYX;
    }

    public int zuoxia(int x,int y,Color c) {//向左下找
        int a=x;
        int b=y;
        for(int i=1;i<5;i++) {
            a--;
            b--;
            if(a<0||b<0|| qizis[a][b]==null) {
                break;
            }else if(qizis[a][b].getColor()==c)
                countZX++;
            else
                break;
        }
        return countZX;
    }

    public int youshang(int x,int y,Color c) {//向右上找
        int a=x;
        int b=y;
        for(int i=1;i<5;i++) {
            a++;
            b++;
            if(a==15||b==15|| qizis[a][b]==null) {
                break;
            }else if(qizis[a][b].getColor()==c)
                countYS++;
            else
                break;
        }
        return countYS;
    }

    public void computerChess() {

        hashMap.put("10000", 15);//眠1连
        hashMap.put("20000", 10);//眠1连

        hashMap.put("20100",17);//眠1连，15
        hashMap.put("10200",12);//眠1连，10
        hashMap.put("21000",15);//眠1连，15
        hashMap.put("12000",10);//眠1连，10
        hashMap.put("20010",19);//眠1连，15
        hashMap.put("10020",14);//眠1连，10
        hashMap.put("20100",17);//眠1连，15
        hashMap.put("10200",12);//眠1连，10
//
//		hashMap.put("00010",21);//活1连，15
//		hashMap.put("00020",16);//活1连，10
//		hashMap.put("00100",19);//活1连，15
//		hashMap.put("00200",14);//活1连，10
//		hashMap.put("01000",17);//活1连，15
//		hashMap.put("02000",12);//活1连，10
//
        //被堵住
        hashMap.put("10100",65);//眠2连，40
        hashMap.put("20200",60);//眠2连，30
        hashMap.put("01100",65);//眠2连，40
        hashMap.put("02200",60);//眠2连，30
        hashMap.put("11000",65);//眠2连，40
        hashMap.put("22000",60);//眠2连，30

        hashMap.put("21010",65);//眠2连，40
        hashMap.put("12020",60);//眠2连，30
        hashMap.put("20110",65);//眠2连，40
        hashMap.put("10220",60);//眠2连，30
        hashMap.put("21100",65);//眠2连，40
        hashMap.put("12200",60);//眠2连，30

//		hashMap.put("01010",75);//活2连，40
//		hashMap.put("02020",70);//活2连，30
//		hashMap.put("00110",75);//活2连，40
//		hashMap.put("00220",70);//活2连，30
//		hashMap.put("01100",75);//活2连，40
//		hashMap.put("02200",70);//活2连，30
//		hashMap.put("11000",75);//活2连，40
//		hashMap.put("00022",70);//活2连，30
//
//		//被堵住
        hashMap.put("11100",150);//眠3连，100
        hashMap.put("22200",140);//眠3连，80
        hashMap.put("21110",150);//眠3连，100
        hashMap.put("12220",140);//眠3连，80
//
//		hashMap.put("10110",1000);//活3连，130
//		hashMap.put("20220",800);//活3连，110
//		hashMap.put("11010",1000);//活3连，130
//		hashMap.put("22020",800);//活3连，110
//		hashMap.put("01110", 1000);//活3连
//		hashMap.put("02220", 800);//活3连

        hashMap.put("11110",3000);//4连，300
        hashMap.put("11112",3000);//4连，300
        hashMap.put("22220",3500);//4连，280
        hashMap.put("22221",3500);//4连，280


        int a;
        int b;
        for(int y=0;y<line;y++) {
            for(int x=0;x<line;x++) {
                if(qizis[x][y]==null) {
                    //向左
                    a=x;
                    for(int i=1;i<6;i++) {
                        a--;
                        if(a<0)
                            break;
                        if(qizis[a][y]!=null) {
                            if(qizis[a][y].getColor()== userColor) {
                                zuo+='2';
                            }else
                                zuo+='1';
                        }else
                            zuo+=0;
                    }
                    Integer integer=hashMap.get(zuo);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向右
                    a=x;
                    for(int i=1;i<6;i++) {
                        a++;
                        if(a==line)
                            break;
                        if(qizis[a][y]!=null) {
                            if(qizis[a][y].getColor()== userColor) {
                                you+='2';
                            }else
                                you+='1';
                        }else
                            you+=0;
                    }
                    integer=hashMap.get(you);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向上
                    b=y;
                    for(int i=1;i<6;i++) {
                        b--;
                        if(b<0)
                            break;
                        if(qizis[x][b]!=null) {
                            if(qizis[x][b].getColor()== userColor) {
                                shang+='2';
                            }else
                                shang+='1';
                        }else
                            shang+=0;
                    }
                    integer=hashMap.get(shang);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向下
                    b=y;
                    for(int i=1;i<6;i++) {
                        b++;
                        if(b==line)
                            break;
                        if(qizis[x][b]!=null) {
                            if(qizis[x][b].getColor()== userColor) {
                                xia+='2';
                            }else
                                xia+='1';
                        }else
                            xia+=0;
                    }
                    integer=hashMap.get(xia);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向左上
                    a=x;
                    b=y;
                    for(int i=1;i<6;i++) {
                        a--;
                        b--;
                        if(a<0||b<0)
                            break;
                        if(qizis[a][b]!=null) {
                            if(qizis[a][b].getColor()== userColor) {
                                zuoshang+='2';
                            }else
                                zuoshang+='1';
                        }else
                            zuoshang+=0;
                    }
                    integer=hashMap.get(zuoshang);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向右下
                    a=x;
                    b=y;
                    for(int i=1;i<6;i++) {
                        a++;
                        b++;
                        if(a==line||b==line)
                            break;
                        if(qizis[a][b]!=null) {
                            if(qizis[a][b].getColor()== userColor) {
                                youxia+='2';
                            }else
                                youxia+='1';
                        }else
                            youxia+=0;
                    }
                    integer=hashMap.get(youxia);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向左下
                    a=x;
                    b=y;
                    for(int i=1;i<6;i++) {
                        a--;
                        b++;
                        if(a<0||b==line)
                            break;
                        if(qizis[a][b]!=null) {
                            if(qizis[a][b].getColor()== userColor) {
                                zuoxia+='2';
                            }else
                                zuoxia+='1';
                        }else
                            zuoxia+=0;
                    }
                    integer=hashMap.get(zuoxia);
                    if(integer!=null)
                        chessValue[x][y]+=integer;

                    //向右上
                    a=x;
                    b=y;
                    for(int i=1;i<6;i++) {
                        a++;
                        b--;
                        if(a==line||b<0)
                            break;
                        if(qizis[a][b]!=null) {
                            if(qizis[a][b].getColor()== userColor) {
                                youshang+='2';
                            }else
                                youshang+='1';
                        }else
                            youshang+=0;
                    }
                    integer=hashMap.get(youshang);
                    if(integer!=null)
                        chessValue[x][y]+=integer;
                    zuo="";
                    you="";
                    shang="";
                    xia="";
                    zuoshang="";
                    zuoxia="";
                    youshang="";
                    youxia="";
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        init();
        if("重新开始".equals(e.getActionCommand())) {
            inte=0;
            for(int ix=0;ix<line;ix++) {//设置所有棋子为null
                for(int iy=0;iy<line;iy++) {
                    if(qizis[ix][iy]!=null) {
                        qizis[ix][iy]=null;

                    }
                }
            }
            modeValue =JOptionPane.showOptionDialog(jFrame, j, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, modes, null);
            colorValue =JOptionPane.showOptionDialog(jFrame, j2, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, colors, null);
            if(modeValue ==JOptionPane.CLOSED_OPTION|| colorValue ==JOptionPane.CLOSED_OPTION) {
                System.exit(1);
            }
            if(colorValue ==1) {
                userColor =Color.black;
                computerColor =Color.white;
            }else {
                userColor =Color.white;
                computerColor =Color.black;
            }
            a=true;
            jFrame.repaint();
        }else {
            if(qizis[prex][prey]==null) {//如果上一步棋子为null了
                JLabel jLabel1 =new JLabel("只能悔一步棋！");
                JOptionPane.showMessageDialog(jFrame, jLabel1, "提示！", JOptionPane.WARNING_MESSAGE);
            }else {
                qizis[prex][prey]=null;
                inte--;
                if(modeValue ==1) {
                    qizis[cx][cy]=null;
                    inte--;
                }else {
                    if(color == userColor)
                        a=true;
                    else
                        a=false;
                }
                jFrame.repaint();
            }
        }
    }

    private void init(){
        count1=0;
        count2=0;
        countS=0;
        countX=0;
        countZS=0;
        countYX=0;
        countZX=0;
        countYS=0;
    }

}
